package com.myastro.main;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.Window;
import android.view.WindowManager;

import com.myastro.R;


public class MainActivity extends Activity 
{
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Create(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Destroy(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		Start(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		Stop(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		Pause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Resume(this);
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	    super.onConfigurationChanged(newConfig);

	    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
	    {
	        //[ ... React to different orientation ... ]
	    }
	    if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) 
	    {
	        //[ ... React to changed keyboard visibility ... ]
	    }
	}  

	//common function start
	static protected void Create(Activity acty)
	{
		acty.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		if (getBrightnessMode(acty) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) 
		{
	       	m_beforeBrightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;	        	
        	///////////////////////////////////////////////////////////////
			//
			Window w = acty.getWindow();
			WindowManager.LayoutParams lp = acty.getWindow().getAttributes();
			lp.screenBrightness = 0.8f;
			w.setAttributes(lp);
			//
			///////////////////////////////////////////////////////////////
        }
	}
	static protected void Start(Activity acty)
	{
		
	}
	static protected void Resume(Activity acty)
	{
		//acty.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    		
		m_beforeBrightnessMode = getBrightnessMode(acty);		
		if (m_beforeBrightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
		{
			
		}
		else if( m_beforeBrightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL )
		{	
			///////////////////////////////////////////////////////////////
			//
			int nCurBright = 0;			
			try {
				nCurBright = android.provider.Settings.System.getInt(acty.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
			} catch (SettingNotFoundException e) {
				//e.printStackTrace();
			}
			
			try 
			{
				Window w = acty.getWindow();
				WindowManager.LayoutParams lp = acty.getWindow().getAttributes();
				lp.screenBrightness = ((float)nCurBright) / 255;
				w.setAttributes(lp);
			}
			catch( Exception e )
			{
				;
			}
			//
			///////////////////////////////////////////////////////////////
		}
	}
	static protected void Pause(Activity acty)
	{
		
	}	
	static protected void Stop(Activity acty)
	{
		
	}	
	static protected void Destroy(Activity acty)
	{
		//acty.finish();
	}
	
  	// H/W back button
   	@Override
   	public void onBackPressed()
   	{
   		AlertDialog.Builder b = new AlertDialog.Builder(this);
   		b.setTitle("Digital Compass");
   		b.setMessage("Exit App?");
   		b.setIcon(R.drawable.icon);
   		
   		b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
		   		finish();
		   		//ExitApp();
			}
		});
   		
   		b.setNegativeButton("No", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				return;
			}
		});
   		
   		b.show();
   	}
	
	
	private void ExitApp() {
		int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
		if (sdkVersion < 8) {
			ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
			am.restartPackage(getPackageName());
		} else {
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
					String name = getApplicationInfo().processName;
					
					while (true) {
						List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
						
						for (RunningAppProcessInfo i : list) {
							if (i.processName.equals(name) == true) {
								if (i.importance >= RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
									am.restartPackage(getPackageName());
									Thread.yield();
									break;
								}
							}
						}
					}
					
				}
				
			}, "Process Killer").start();
		}
	}
	
	
	static protected int getBrightnessMode(Activity acty) {
		return Integer.parseInt(Settings.System.getString(acty.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE));
	}
	private static int m_beforeBrightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
}